package org.colin.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.apache.shiro.crypto.hash.Hash;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.DEntry;
import org.colin.dictPojo.DLanguage;
import org.colin.service.DEntryService;
import org.colin.service.DLanguageService;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Controller
@RequestMapping("/dLanguage")
@Api(tags = "基础数据管理-语种管理")
public class DLanguageController {

    @Autowired
    DLanguageService dLanguageService;
    @Autowired
    DEntryService dEntryService;


    @ApiOperation(value = "新增语种接口")
    @LogAnnotation(title = "基础数据管理", action = "新增语种接口")
    @PostMapping("addLanguage")
    @ResponseBody

    public DataResult addLanguage(@ApiParam(name="语种类",value="语种对象",required=true) DLanguage dLanguage){
        String languageName = dLanguage.getLanguageName();
        if(languageName == null || languageName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "保存失败,语种名称不能为空");
        }
        Integer type = dLanguage.getType();
        if(type == null || type <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "保存失败,语种类型不能为空");
        }
        boolean save = dLanguageService.addLanguage(dLanguage);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败");
        }
    }


    @ApiOperation(value = "修改语种接口")
    @PutMapping("updateLanguage")
    @LogAnnotation(title = "基础数据管理", action = "修改语种接口")
    @ResponseBody

    public DataResult updateLanguage(@ApiParam(name="语种类",value="对象",required=true) DLanguage dLanguage){
        HashMap<String, Object> hashMap = new HashMap<>();

        Integer id = dLanguage.getId();
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,唯一标识不能为空");
        }

        int count = dEntryService.selectCountByTableName(id);
        if(count > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,语种下有词条，禁止修改");
        }
        String languageName = dLanguage.getLanguageName();
        if(languageName == null || languageName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,语种名称不能为空");
        }
        Integer type = dLanguage.getType();
        if(type == null || type <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,语种类型不能为空");
        }
        dLanguage.setType(null);
        boolean save = dLanguageService.updateById(dLanguage);
        if(save == true){
            return  DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败");
        }
    }

    @ApiOperation(value = "批量删除语种接口")
    @LogAnnotation(title = "基础数据管理", action = "批量删除语种接口")
    @DeleteMapping("deleteLanguage")
    @ResponseBody

    public DataResult deleteLanguage(@ApiParam(name="id",value="[1,2,3]",required=true)String id){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "删除失败,唯一标识不能为空");
        }
        Integer delete = dLanguageService.deleteLanguage(ids);
        if(delete == 1){
           return DataResult.success();
        }else if(delete == 2){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode() , "删除失败，语种下有词条，禁止删除");
        }else if(delete == 2){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode() , "删除失败，题目下有词条，禁止删除");
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode() , "删除失败");
        }
    }

    @ApiOperation(value = "查询语种详情")
    @LogAnnotation(title = "基础数据管理", action = "查询语种详情")
    @GetMapping("selectLanguageById")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "唯一标识", name = "id" ,required=true ,  paramType = "query"),
    })
    @ResponseBody
    public DataResult selectLanguageById(Integer id){
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "查询失败,唯一标识不能为空");
        }
        DLanguage byId = dLanguageService.getById(id);
        DataResult success = DataResult.success();
        success.setData(byId);
        return success;
    }


    @ApiOperation(value = "查询语种-分页")
    @LogAnnotation(title = "基础数据管理", action = "查询语种-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="当前页" ,required=true, paramType = "query"),
            @ApiImplicitParam(name="pageSize",value="每页数量" ,required=true, paramType = "query")
    })
    @GetMapping("selectLanguageIsPage")
    @ResponseBody
    public DataResult selectLanguageById(Integer page , Integer pageSize ){
        HashMap<String, Object> hashMap = new HashMap<>();
        Page<DLanguage> dLanguages = new Page<>(page, pageSize);
        IPage<DLanguage> list = dLanguageService.page(dLanguages);
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }


    @ApiOperation(value = "查询语种--不分页")
    @GetMapping("selectLanguageIsNotPage")
    @LogAnnotation(title = "基础数据管理", action = "查询语种--不分页")
    @ResponseBody
    public DataResult selectLanguageById(){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<DLanguage> list = dLanguageService.list();
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }

}

